package cachex

import (
	"context"

	"github.com/go-redis/redis/v8"
	"github.com/tal-tech/go-zero/core/stores/cache"
)

type Store interface {
	Get(k string) (string, error)
	Set(k string, v interface{}) error
}

type red struct {
	client *redis.Client
}

var ctx = context.Background()

func NewStore(c cache.CacheConf, db int) Store {
	ci := redis.NewClient(&redis.Options{
		Addr:     c[0].Host,
		Password: c[0].Pass,
		DB:       db,
	})
	return &red{
		client: ci,
	}
}

func (r *red) Get(k string) (string, error) {
	return r.client.Get(ctx, k).Result()
}

func (r *red) Set(k string, v interface{}) error {
	return r.client.Set(ctx, k, v, 0).Err()
}
